﻿//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
 **************************************************************************
 */

/**
 Navigation MainFrm鼠标事件接口类
 */
define(function (require, exports, module) {
    /**
     * Mainframe event handler for navigation panel plugin.
     * @param {WebPDF.NavigationPanelPlugin} navigationPanel navigation panel plugin
     * @constructor
     */
    var NavigationPanelMainFrmEventHandler = function (navigationPanel) {
        this._navigationPanel = navigationPanel;
        this._isMouseDown = false;
            this._oldPosX = 0;
        this._minLeftPanelWidth = 170;
        var _self = this;
        $("#" + _self._navigationPanel.getSplitterDivID()).mousedown(function (event) {
            WebPDF.Common.preventDefaults(event, true);
            _self._isMouseDown = true;
            _self._oldPosX = event.pageX;

            var panelWidth = _self._navigationPanel.getPanelWidth();
            var splitterWidth = $(this).width();
            $("#" + _self._navigationPanel.getMoveSplitterDivID()).css({ left: panelWidth - splitterWidth });
            return true;
        });
    };

    NavigationPanelMainFrmEventHandler.prototype =
    {
        getType: function () {
            return "Navigation Panel";
        },
        onLButtonDown: function (event) {
            return false;
        },
        onLButtonUp: function (event) {
            if (this._isMouseDown) {
                var panelWidth = this._navigationPanel.getPanelWidth();
                var newWidth = panelWidth + event.pageX - this._oldPosX;
                var jqTreePanel = $("#" + this._navigationPanel.getTreePanelDivID());
                var oldTreePanelWidth = jqTreePanel.width();
                var newTreePanelWidth = oldTreePanelWidth + event.pageX - this._oldPosX;
                if (newWidth < this._minLeftPanelWidth) {
                    this._isMouseDown = false;
                    this._navigationPanel.changePanelState(true);
                    $("#" + this._navigationPanel.getMoveSplitterDivID()).css({ left: -10000 });
                    jqTreePanel.width(newTreePanelWidth);
                    this._navigationPanel.onSize();
                    return true;
                }
                $("#" + this._navigationPanel.getMoveSplitterDivID()).css({ left: -10000 });
                jqTreePanel.width(newTreePanelWidth);

                this._navigationPanel.onSize();
                this._isMouseDown = false;
                return true;
            }
            this._isMouseDown = false;
            return false;
        },
        onMouseMove: function (event) {
            if (!this._isMouseDown) return false;
            WebPDF.Common.preventDefaults(event, true);
            var panelWidth = this._navigationPanel.getPanelWidth();
            var tabPanelWidth = $("#" + this._navigationPanel.getTabPanelDivID()).width();
            var splitterWidth = $("#" + this._navigationPanel.getSplitterDivID()).width();
            var newWidth = panelWidth + event.pageX - this._oldPosX;
            if (newWidth < tabPanelWidth + splitterWidth) {
                return true;
            }
            $("#" + this._navigationPanel.getMoveSplitterDivID()).css({ left: newWidth - splitterWidth });
            return true;
        },
        onMouseOut: function (event) {
            return false;
        },
        onMouseLeave: function (event) {
            return this.onLButtonUp(event);
        }
    };

    WebPDF.Event.NavigationPanelMainFrmEventHandler = NavigationPanelMainFrmEventHandler;
    return WebPDF.Event.NavigationPanelMainFrmEventHandler;
});